//
//  StarView.m
//  MyPopStar
//
//  Created by tarena on 16/1/7.
//  Copyright © 2016年 ssnb. All rights reserved.
//

#import "StarView.h"

@interface StarView ()
@property (nonatomic,strong) UIImageView *bgImage;
@property (nonatomic,assign) StarPoint point;
@property (nonatomic,strong) NSString *name;
@end
@implementation StarView
mdealloc

#pragma mark --用starModel 来初始化

-(instancetype)initWithFrame:(CGRect)frame andStarType:(StarModelType)type andPoint:(StarPoint)point{
    if (self = [super init]) {
        self.frame =CGRectMake(frame.origin.x, frame.origin.y-viewHeight, starSide, starSide);
        self.bgImage.frame =CGRectMake(0, 0, starSide, starSide);
        self.bgImage.image =[UIImage imageNamed:[self starImageName:type]] ;
        self.point =point;
        [self animationWith:frame];
    }
    return  self;
}

-(NSString*)starImageName:(StarModelType)type{
    NSString *imgName;
    NSString *dielName;
    switch (type) {
        case StarModelTypeRed:
            imgName = @"star_r";
            dielName =@"star3d";
            break;
        case StarModelTypeBlue:
            imgName = @"star_b";
            dielName =@"star4d";
            break;
        case StarModelTypePink:
            imgName = @"star_p";
            dielName =@"star5d";
            break;
        case StarModelTypeGreen:
            imgName = @"star_g";
            dielName =@"star2d";
            break;
        case StarModelTypeYellow:
            imgName = @"star_y";
            dielName =@"star1d";
            break;
        default:
            imgName =@"";
            break;
    }
    self.name=dielName;
    return imgName;
}

-(void)animationWith:(CGRect)frame {
    WeakSelf(content);
    [UIView animateWithDuration:0.5 delay:0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
        content.frame = frame;
    } completion:^(BOOL finished) {
    }];
}

-(void)updateStatus:(CGRect)frame andPoint:(StarPoint)point{
    self.point = point;
    WeakSelf(content);
    [UIView animateWithDuration:0.5 delay:0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
        content.frame = frame;
    } completion:^(BOOL finished) {
    }];
}

-(void)remove{
    [self removeFromSuperview];
}
#pragma mark -- 点击的时候的闪闪
-(void)starClickAnimation{
    CABasicAnimation *animation  = [CABasicAnimation animation];
    animation.duration =0.5;//=CGFLOAT_MAX;
    animation.keyPath =@"bounds" ;
    animation.fromValue=[NSValue valueWithCGRect:self.frame];
    animation.toValue =[NSValue valueWithCGRect:CGRectMake(3, 3, starSide-6, starSide-6)];
    animation.autoreverses=YES;
    animation.repeatCount=CGFLOAT_MAX;
    [self.bgImage.layer addAnimation:animation forKey:@"shanshan"];
}
-(void)stopClickAnimation{
    [self.bgImage.layer removeAnimationForKey:@"shanshan"];
}
-(void)starDieAnimation{
    //当消失的时候显示
    
}
#pragma mark --点击对象的时候返回到上一层
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (self.delegate !=nil) {
        [self.delegate star:self touchPoint:self.point];
    }
}

-(NSString *)dielImgName{
    return self.name;
}
#pragma  mark --懒加载
- (UIImageView *)bgImage {
	if(_bgImage == nil) {
		_bgImage = [[UIImageView alloc] init];
        [self addSubview:_bgImage];
	}
	return _bgImage;
}
@end
